// pages/orderDetail/orderDetail.js
var app = getApp()
Page({
  data: {
    myOrder: [],
    notShow: '',
    remind: false,
    done: false
  },

  goToOrder(e) {
    // console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '../orderView/orderView?id=' + e.currentTarget.id,
    })
  },

  onLoad: function (options) {
    var list = JSON.parse(options.List)
    // console.log(list)
    let that = this
    if (list.length != 0 && list[0].state === '待发货') {
      that.setData({
        remind: true
      })
    }
    if (list.length != 0 && list[0].state === '已收货') {
      that.setData({
        done: true
      })
    }
    if (list.length != 0) {
      that.setData({
        myOrder: list,
      })
    } else {
      that.setData({
        notShow: true
      })
    }
  },
  remindDeliverGoods(e) {
    let that = this
    let list = this.data.myOrder
    wx.cloud.database().collection('orderList').doc(e.currentTarget.dataset.id).update({
      data: {
        state: '待收货'
      },
      success: function (res) {
        list.splice(e.currentTarget.dataset.index, 1)
        // 判断list为空时，准确显示空状态
        if(list.length == 0){
          that.setData({
            notShow:true
          })
        }
        that.setData({
          myOrder: list
        })
      }
    })
  },
  enterDoneCart(e) {
    let that = this
    let list = this.data.myOrder
    let myOrder = []
    for (let i = 0; i < list.length; i++) {
      if (list[i]._id == e.currentTarget.dataset.id) {
        for (let j = 0; j < list[i].myOrder.length; j++) {
          list[i].myOrder[j].evaluate = "待评价"
          list[i].myOrder[j].myOrderId = list[i]._id,
            list[i].myOrder[j].myOrderIndex = j
        }
        myOrder = list[i].myOrder
      }
    }

    wx.cloud.database().collection('orderList').doc(e.currentTarget.dataset.id).update({
      data: {
        state: '已收货',
        myOrder: myOrder
      },
      success: function (res) {
        list.splice(e.currentTarget.dataset.index, 1)
        if(list.length == 0){
          that.setData({
            notShow:true
          })
        }
        that.setData({
          myOrder: list
        })
      }
    })
  }
})